package com.tianqu.tms.www.transfer.controller;

import java.util.ArrayList;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

/**
 * 库存车辆受理-车辆一览 controller
 * @author mufeiyu
 *
 */
public class CarStockAcceptListController extends TmsController {
	private Logger logger = Logger.getLogger(CarStockAcceptListController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			TransferRgInputViewHelper viewHelper = new TransferRgInputViewHelper();
			TransferRgActionForm form = (TransferRgActionForm)uc.getInputForm();
			String serial = "";
			String licensePlate = "";
			String vin = "";
			if(!StringUtil.isNullOrBlank((String)uc.getAttribute("carSerial"))){
				serial = (String)uc.getAttribute("carSerial");
			}else {
				serial = form.getSerial();
			}
			if(!StringUtil.isNullOrBlank((String)uc.getAttribute("licensePlate"))){
				licensePlate = (String)uc.getAttribute("licensePlate");
			}else {
				licensePlate = form.getLicensePlate();
			}
			if(!StringUtil.isNullOrBlank((String)uc.getAttribute("vin"))){
				vin = (String)uc.getAttribute("vin");
			}else {
				vin = form.getVin();
			}
			Car car = Car.getInstance();
			List carList = new ArrayList();
			//查询车辆一览表信息
			if(!StringUtil.isNullOrBlank(serial)){
				car.setSerial(serial);
				carList = car.findCarListInfo(car,1);//根据车辆编号查询
			}else if(!StringUtil.isNullOrBlank(licensePlate)){
				car.setLicensePlate(licensePlate);
				carList = car.findCarListInfo(car,2);//根据车牌号码查询
			}else if(!StringUtil.isNullOrBlank(vin)){
				car.setVin(vin);
				carList = car.findCarListInfo(car,3);//根据车架号查询
			}
			
			//查询车辆是否过户受理
			TransferAcceptedInfo transferInfo = TransferAcceptedInfo.getInstance();
			List carStockInfoList = new ArrayList();
			if(carList.size()>0){
				for(int i = 0;i<carList.size();i++){
					Car carInfo = new Car();
					carInfo = (Car)carList.get(i);
					if(transferInfo.isExistTransferInfoByBasicCarId(carInfo.getSerial())){						
						carInfo.setIsExistFlag(1);
						transferInfo = transferInfo.findTransferInfoByBasicCarId(carInfo.getSerial());
						if(transferInfo.getLc_printNum() != 0 && transferInfo.getCy_printNum() != 0){
							carInfo.setCylc_printNum(0);
						}else {
							carInfo.setCylc_printNum(1);
							carInfo.setLc_printNum(transferInfo.getLc_printNum());
							carInfo.setCy_printNum(transferInfo.getCy_printNum());							
						}
					}else {
						carInfo.setIsExistFlag(0);
					}
					carStockInfoList.add(carInfo);
				}
				viewHelper.setCarStockInfoList(carStockInfoList);
				uc.setAttribute("emptyFlag", "false");
				uc.setAttribute("carSerial", serial);
				uc.setAttribute("licensePlate", licensePlate);
				uc.setAttribute("vin", vin);
			}else {
				uc.setAttribute("emptyFlag", "true");
			}						
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
